Modify choice value (DBE)

Hi everyone !

 

I'm trying to develop a script that display two choice list. The first one contains attributes of the current module, and the second contains values of the selected attribute.

I'm able to populate the first choice list that contains attributes. But the second choice list need to be updated each time an attribute has been selected in the first choice list.

Today I'm also able to update this second choice list, but the array of the choice list is declared with the max size of possible values found in all enumerated type.In my example, I have an attribute where its type contains 53 values. So the array is created with 53 values. Then when I select another attribute I still have 53 choice in the list :

 

This is not really user friendly !

If someone know how to deal with it ! In fact I would like to obtain the same result than when you want to set a filter on the view ! 

Here is the code I use :

Module m = current Module

if(null m)
{
        infoBox("Please run the script in module context.")
        halt()  
}

Skip listAttr = create

//loop on all attribute to create list + get the type with the maximum size of value (if enum)
int nbAttr = 0, maxLengthType = 0
AttrDef attrModule
AttrType typeAttr
for attrModule in m do
{
    if(!attrModule.system)
    {
        put(listAttr,attrModule.name,attrModule.name)
        nbAttr++

        //get type information
        typeAttr = attrModule.type
        if(typeAttr.type"" == "Enumeration")
        {
            if(typeAttr.size > maxLengthType)
            {
                maxLengthType = typeAttr.size
               
            }
        }
    }
}

//fill the array for the choice DBE
string choiceAttr[nbAttr] 
int index = 0
string attrName 
for attrName in listAttr do
{
    choiceAttr[index] = attrName
    index++
}
sort(choiceAttr)
delete(listAttr)

string choiceEnum[maxLengthType]
for (i = 0; i<maxLengthType;i++)
        choiceEnum[i] = null


//GUI
DB     mainDB
DBE    choiceAttrDBE, choiceEnumDBE

void updateValuesOfAttr(string attr)
{
        string enumVal
        for(i = 0; i<maxLengthType; i ++)
        {
                        choiceEnum[i] = null
        }
        AttrDef aD = find(m,attr) //can not be null so no test...
        AttrType aT = aD.type
        if(aT.type"" == "Enumeration")
        {
                int nbEnumValues = aT.size
                for(j = 0;j<nbEnumValues;j++)
                {
                        choiceEnum[j] = aT.strings[j]""
                } 
                set(choiceEnumDBE,choiceEnum)
        }else{
                choiceEnum[0]
                set(choiceEnumDBE,choiceEnum)
        }
}

void choiceCallBack(DBE dbe)
{ // General callBack routine for the choice DBE
  int    i = get dbe
  string s = get dbe
  string currVal = ""
  if (i == -1){currVal = s}
  else
  { 
    if (dbe == choiceAttrDBE)
    {
        //Attribute selection => update choice list of "Values" choice DBE
        updateValuesOfAttr(choiceAttr[i])
                
    }
    else if (dbe == choiceEnumDBE)
    {
        currVal = choiceEnum[i]
        print "Valeur selectionnée : "currVal"\n"
    }
  }
}

mainDB = create "test"
choiceAttrDBE = choice(mainDB, "Attribute : ", choiceAttr, 0, 30, false)
choiceEnumDBE = choice(mainDB, "Value : ", choiceEnum, 0, 30, true)
set(choiceAttrDBE, choiceCallBack)
set(choiceEnumDBE, choiceCallBack)
updateValuesOfAttr(choiceAttr[0])//attribute 0 is the first selected so update enum values according to this attribute for init
show mainDB

 

 

Thanks in advance !

 


Joooris - Fri Jun 16 03:57:55 EDT 2017

Re: Modify choice value (DBE)
O.Wilkop - Fri Jun 16 04:12:33 EDT 2017

Hey,

 

I recommend you use 

empty(choiceEnumDBE)

and 

insert(choiceEnumDBE, noElems(choiceEnumDBE), aT.strings[j]"")

 

instead of using set(choiceEnumDBE,choiceEnum). This way you clear the whole choice DBE and repopulate it with the new values each time.

Re: Modify choice value (DBE)
Joooris - Fri Jun 16 05:51:51 EDT 2017

O.Wilkop - Fri Jun 16 04:12:33 EDT 2017

Hey,

 

I recommend you use 

empty(choiceEnumDBE)

and 

insert(choiceEnumDBE, noElems(choiceEnumDBE), aT.strings[j]"")

 

instead of using set(choiceEnumDBE,choiceEnum). This way you clear the whole choice DBE and repopulate it with the new values each time.

Hi,

 

Thanks O.Wilkop, indeed it's better !

I post the updated code in case someone need it !

Module m = current Module

if(null m)
{
        infoBox("Please run the script in module context.")
        halt()  
}

Skip listAttr = create

//loop on all attribute to create list + get the type with the maximum size of value (if enum)
int nbAttr = 0, maxLengthType = 0
AttrDef attrModule
AttrType typeAttr
for attrModule in m do
{
    if(!attrModule.system)
    {
        put(listAttr,attrModule.name,attrModule.name)
        nbAttr++

        //get type information
        typeAttr = attrModule.type
        if(typeAttr.type"" == "Enumeration")
        {
            if(typeAttr.size > maxLengthType)
            {
                maxLengthType = typeAttr.size
               
            }
        }
    }
}

//fill the array for the choice DBE
string choiceAttr[nbAttr] 
int index = 0
string attrName 
for attrName in listAttr do
{
    choiceAttr[index] = attrName
    index++
}
sort(choiceAttr)
delete(listAttr)

string choiceEnum[maxLengthType] = {""}
for (i = 0; i<maxLengthType;i++)
        choiceEnum[i] = null

//GUI
DB     mainDB
DBE    choiceAttrDBE, choiceEnumDBE

void updateValuesOfAttr(string attr)
{
        empty(choiceEnumDBE)
        AttrDef aD = find(m,attr) //can not be null so no test...
        AttrType aT = aD.type
        if(aT.type"" == "Enumeration")
        {
                int nbEnumValues = aT.size
                for(j = 0;j<nbEnumValues;j++)
                {
                        choiceEnum[j] = aT.strings[j]""
                        insert(choiceEnumDBE, noElems(choiceEnumDBE), aT.strings[j]"")
                }
        }
        else
        {
                choiceEnum[0] = "\"Enter a value\""
                insert(choiceEnumDBE, noElems(choiceEnumDBE),"\"Enter a value\"")
        }
        set(choiceEnumDBE,0)//always display first choice of enumerated values
}

void choiceCallBack(DBE dbe)
{ // General callBack routine for the choice DBE
  int    i = get dbe
  string s = get dbe
  string currVal = ""
  if (i == -1){currVal = s}
  else
  { 
    if (dbe == choiceAttrDBE)
    {
        //Attribute selection => update choice list of "Values" choice DBE
        updateValuesOfAttr(choiceAttr[i])
    }
    else if (dbe == choiceEnumDBE)
    {
        currVal = choiceEnum[i]
        print "Valeur selectionnée : "currVal"\n"
    }
  }
}

mainDB = create " - Filter"
choiceAttrDBE = choice(mainDB, "Attribute : ", choiceAttr, 0, 30, false)
choiceEnumDBE = choice(mainDB, "Value : ", choiceEnum, 0, 30, true)
set(choiceAttrDBE, choiceCallBack)
set(choiceEnumDBE, choiceCallBack)
realize(mainDB)//Realize the view or we can not update the choiceEnumDBE with first attribute
updateValuesOfAttr(choiceAttr[0])//Update with the first attribute's values
show mainDB